package com.tinyseed.emcp.api.basicdata.service.impl;

import com.tinyseed.emcp.api.app.common.UuidUtil;
import com.tinyseed.emcp.api.basicdata.domain.Industry;
import com.tinyseed.emcp.api.basicdata.model.IndustryInputDTO;
import com.tinyseed.emcp.api.basicdata.model.IndustryOutputDTO;
import com.tinyseed.emcp.api.basicdata.repository.IndustryRepository;
import com.tinyseed.emcp.api.basicdata.service.IndustryService;
import com.tinyseed.emcp.api.basicdata.transfer.IndustryTransfer;
import com.tinyseed.hmxx.common.base.PageData;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Jarvis on 2019/11/18.
 */

@Service
public class IndustryServiceImpl implements IndustryService {

    @Autowired
    IndustryRepository industryRepository;

    @Override
    public List<IndustryOutputDTO> findIndustryByPage(int current, int size) {
        Page<Industry> industries = industryRepository.findAll(PageRequest.of(current, size));
        List<IndustryOutputDTO> industryOutputDTOS = new ArrayList<>();
        industries.getContent().forEach(temp -> industryOutputDTOS.add(temp.convertToIndustryOutputDTO()));
        return industryOutputDTOS;
    }

    @Override
    public IndustryOutputDTO save(Industry industry) {
        Timestamp nowTime = new Timestamp(System.currentTimeMillis());
        if (industry.getId() == null || industry.getCreateTime() == null) {
            industry.setIndustryCode(UuidUtil.generateUUID());
            industry.setCreateTime(nowTime);
        }
        industry.setModifyTime(nowTime);
        Industry model = industryRepository.save(industry);
        return IndustryTransfer.INSTANCE.mapModel2OutPut(model);
    }

    @Override
    public Integer delete(Long id) {
        industryRepository.deleteById(id);
        return 1;
    }

    @Override
    public PageData page(int size, int current, IndustryInputDTO industryInputDTO) {
        Pageable pageable = PageRequest.of(current, size, Sort.Direction.DESC, "modifyTime");
        Page<Industry> result = industryRepository.findAll((Root<Industry> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) -> {
            Predicate predicate = null;
            if (industryInputDTO.getIndustryName() != null && !"".equals(industryInputDTO.getIndustryName())) {
                predicate = criteriaBuilder.like(root.get("industryName").as(String.class), "%" + industryInputDTO.getIndustryName() + "%");
            }
            if (industryInputDTO.getStartCreateTime() != null && !"".equals(industryInputDTO.getStartCreateTime())) {
                predicate = criteriaBuilder.and(criteriaBuilder.between(root.get("createTime").as(String.class), industryInputDTO.getStartCreateTime(), industryInputDTO.getEndCreateTime()));
            }
            return predicate;
        }, pageable);
        List<IndustryOutputDTO> inDustyOutputDOTList = new ArrayList<>();
        result.getContent().forEach(temp -> inDustyOutputDOTList.add(IndustryTransfer.INSTANCE.mapModel2OutPut(temp)));
        PageData pageData = new PageData();
        pageData.setStart(result.getNumber());
        pageData.setData(inDustyOutputDOTList);
        pageData.setTotal(result.getTotalElements());
        pageData.setLimit(result.getSize());
        return pageData;
    }

    @Override
    public List<IndustryOutputDTO> getOptionList() {
        List<Industry> list = industryRepository.findAll();
        List<IndustryOutputDTO> industryOutputDTOList = new ArrayList<>();
        list.forEach(industry -> {
            IndustryOutputDTO industryOutputDTO = new IndustryOutputDTO();
            BeanUtils.copyProperties(industry, industryOutputDTO);
            industryOutputDTOList.add(industryOutputDTO);
        });
        return industryOutputDTOList;
    }
}
